package com.gaolang.web.controller;


import com.gaolang.web.entities.ControllerEndpoint;
import com.gaolang.web.entities.Health;
import com.gaolang.web.entities.User;
import com.gaolang.web.handler.BusinessException;
import com.gaolang.web.response.Result;
import com.gaolang.web.service.IHealthService;
import com.gaolang.web.service.IUserService;
import com.gaolang.web.vo.HealthVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaolang
 * @since 2021-03-13
 */
@RestController
@RequestMapping("/health")
@Api(value = "健康模块",tags = "健康报备接口")
public class HealthController {

    @Autowired
    private IHealthService healthService;

    @Autowired
    private IUserService userService;

    /**
     * 健康上报
     * @param healthVO
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "健康上报失败", operation = "健康上报")
    @ApiOperation(value = "健康上报",notes = "用户健康上报")
    @PostMapping("/report")
    public Result report(@Validated @RequestBody HealthVO healthVO, Principal principal) throws BusinessException {
        String name = principal.getName();
        User user = userService.getUserByUsername(name);
        healthVO.setUserId(user.getId());
        healthService.report(healthVO);
        return Result.ok();
    }

    /**
     * 今日是否已报备
     * @return
     */
    @ApiOperation(value = "是否报备",notes = "今日是否已报备")
    @GetMapping("/isReport")
    public Result isReport(Principal principal){
        String name = principal.getName();
        User user = userService.getUserByUsername(name);
        Health report = healthService.isReport(user.getId());
        return Result.ok().data("report",report);
    }


}
